package org.liaohailong.victorlibrary.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.jetbrains.annotations.Nullable;
import org.liaohailong.library.db.SQLite;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;
import org.liaohailong.victorlibrary.model.Student;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Author: Victor
 * Date: 2019/5/21 10:36
 * Description: SQLite数据库创建
 */
public class SQLiteActivity extends BaseActivity {
    private static final String TAG = "SQLiteActivity";

    public static void show(Context context) {
        Intent intent = new Intent(context, SQLiteActivity.class);
        context.startActivity(intent);
    }

    private TextView mInfoTxt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite);
        mInfoTxt = findViewById(R.id.info_txt);
    }

    public void insert(View view) {
        Student student = new Student();
        student.setName("小红");
        student.setAge(8);
        student.setSex(0);

        int row = SQLite.INSTANCE.table(Student.class).insert(student);
        Log.i(TAG, "insert row = " + row);
        showResult("成功增加" + row + "行");
    }

    private static final int MULTIPLE_INSERT_COUNT = 10000 * 10;

    public void multipleInsert(View view) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("正在插入");
        progressDialog.setCancelable(false);
        progressDialog.show();
        AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                Student student = new Student();
                student.setName("十萬數據");
                student.setNumber(10086);
                student.setAge(8);
                student.setSex(0);
                List<Student> insertData = new LinkedList<>();
                for (int i = 0; i < MULTIPLE_INSERT_COUNT; i++) {
                    insertData.add(student);
                }
                final long time = System.currentTimeMillis();
                final int total = SQLite.INSTANCE.table(Student.class).multipleInsert(insertData);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.hide();
                        long costTime = System.currentTimeMillis() - time;
                        showResult("插入完毕，共完成" + total + "条，共耗时：" + costTime + "毫秒");
                    }
                });
            }
        });
    }

    public void delete(View view) {
//        Map<String, Object> where = new HashMap<>();
//        where.put("name", "小红");

        int row = SQLite.INSTANCE.table(Student.class)
//                .where(where)
                .delete();
        Log.i(TAG, "delete row = " + row);
        showResult("成功删除" + row + "行");
    }

    public void update(View view) {
        Map<String, Object> set = new HashMap<>();
        set.put("name", "小明");
        set.put("age", 9);
        set.put("sex", 1);
        Map<String, Object> where = new HashMap<>();
        where.put("name", "小红");

        int row = SQLite.INSTANCE.table(Student.class)
                .set(set)
                .where(where)
                .update();
        Log.i(TAG, "update row = " + row);
        showResult("成功改动" + row + "行");
    }

    public void select(View view) {
        List<String> columns = new LinkedList<>();
        columns.add("id");
        columns.add("name");

        List<String> orders = new LinkedList<>();
        orders.add("id");

        List<Student> select = SQLite.INSTANCE
                .table(Student.class)
                .distinct(true)
                .select(columns)
                .order(orders, SQLite.Options.OrderBy.ASC)
                .limit(3)
                .offset(2)
                .select();
        StringBuilder sb = new StringBuilder();
        for (Student student : select) {
            String s = student.toString();
            Log.i(TAG, "student = " + s);
            sb.append(s).append("\n");
        }
        showResult(sb.toString());
    }

    private void showResult(String text) {
        if (mInfoTxt != null) {
            mInfoTxt.setText(text);
        }
    }
}
